from __future__ import absolute_import
from forest.plugins import PluginRegistry, search_plugins, PluginMeta, registry
from os.path import dirname, join
from forest.signals import Signal
import logging
logger = logging.getLogger('forest.tool')
logger.setLevel(logging.ERROR)

TOOLS_REGISTRY_PREFIX = 'tools.'

class Tool(object):
    SIGNAL_TOOL_START = Signal()
    ''' This signal is called when a tool is started '''
    
    SIGNAL_TOOL_PROGRESS = Signal()
    ''' This signal is called when a tool makes progress.
        
        Its sig is handle_progress(tool, progressdata=None, level=None, message='')
    '''
    
    SIGNAL_TOOL_FINISH = Signal()
    ''' This signal is called when a tool is finished '''

    
# some default logging handlers
def default_handle_tool_start(tool, level=None, message=''):
    logger.info('%s: %s', tool.__register__, message)
    
def default_handle_tool_finish(tool, level=None, message=''):
    #logger.info('%s: %s', tool.__register__, message)
    pass
    
def default_handle_tool_progress(tool, progressdata=None, level=None, message=''):
    
    if not level:
        level = logging.INFO
        
    if message:
        logger.log(level, message)


Tool.SIGNAL_TOOL_START.connect(default_handle_tool_start)
Tool.SIGNAL_TOOL_PROGRESS.connect(default_handle_tool_finish)
Tool.SIGNAL_TOOL_FINISH.connect(default_handle_tool_progress)


def search_tools():
    search_plugins(join(dirname(__file__), 'tools'), 'forest.tools.')
    #return tool_registry
    
    
